package edu.clemson.wcbeck.cft;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.xml.sax.SAXException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class CFTTeamNews extends Activity{

	ProgressDialog myProgressDialog = CFTTeamTabs.myProgressDialog;
	MediaPlayer mediaPlayer = CFTTeamSongs.mediaPlayer;
	ArrayAdapter<RssItem> adapter = null;
	ListView listview;
	TextView textview;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.articles_list);
        
        
        myProgressDialog = new ProgressDialog(CFTTeamNews.this);
        myProgressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        myProgressDialog.setMessage("Loading...");
        //myProgressDialog.setIcon(R.drawable.blind);
        myProgressDialog.show();

        
        textview = (TextView) findViewById(R.id.txtLoadingArticles);
        listview = (ListView) findViewById(R.id.lstArticles);
        //TextView textview = new TextView(this);
        //textview.setText("This is the team news tab");
        
        URL url = null;
		try {
			url = new URL("http://search.twitter.com/search.rss?q=search%23love");//http://ncaa.com/newsrss/football");
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        RssFeed feed = null;
		try {
			feed = RssReader.read(url);
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        ArrayList<RssItem> rssItems = feed.getRssItems();
        for(RssItem rssItem : rssItems) {
            Log.i("RSS Reader", rssItem.getTitle());
        }
        
        adapter = new ArrayAdapter<RssItem>(this, R.layout.list_item, rssItems);
        listview.setAdapter(adapter);
        registerForContextMenu(listview);
        
        myProgressDialog.dismiss();
        textview.setText("Finished loading articles.");
    }
    
    @Override
    public void onBackPressed() {
    	// TODO Auto-generated method stub
    	if(mediaPlayer.isPlaying()) { mediaPlayer.stop(); }
    	super.onBackPressed();
    }
}
